package service

import (
	"fmt"

	//"github.com/astaxie/beego/orm"

	"mutu/misc/log"
	//"mutu/misc/typedef"
	"mutu/models"
)

type CategoryManager struct {
}

var (
	CategoryService = CategoryManager{}
)

func (p CategoryManager) GetAllCategorys() ([]*models.Category, error) {
	var arr []*models.Category
	_, err := models.Orm.QueryTable("Category").All(&arr)
	if err != nil {
		log.Log.Error("读取所有分类出错，%s", err)
		return nil, err
	}
	return arr, nil
}

func (p CategoryManager) GetChildren(id int) ([]*models.Category, error) {
	var arr []*models.Category
	_, err := models.Orm.QueryTable("Category").Filter("ParentId", id).All(&arr)
	if err != nil {
		log.Log.Error("读取<%d>子分类出错，%s", id, err)
		return nil, err
	}
	return arr, nil

}

func (p CategoryManager) GetParent(id int) (*models.Category, error) {
	item := &models.Category{
		Id: id,
	}
	err := models.Orm.Read(item)
	if err != nil {
		log.Log.Error("读取<%d>父分类出错，%s", id, err)
		return nil, err
	}

	parent := &models.Category{
		Id:       item.ParentId,
		ParentId: -1,
		Name:     "root",
	}
	if parent.Id != 0 {
		err = models.Orm.Read(parent)
		if err != nil {
			log.Log.Error("读取<%d>父分类出错，%s", id, err)
			return nil, err
		}
	}

	return parent, nil

}

func (p CategoryManager) GetCategoryById(id int) (*models.Category, error) {
	item := &models.Category{
		Id: id,
	}
	err := models.Orm.Read(item)
	if err != nil {
		log.Log.Error("读取<%d>分类出错，%s", id, err)
		return nil, err
	}

	return item, nil

}

func (p CategoryManager) CreateRelCategory(catid int, flag int, relid string) error {
	if catid <= 0 {
		return nil
	}

	catm := &models.CategorysMap{
		CategoryId: catid,
		Flag:       flag,
		RelId:      relid,
	}
	_, err := models.Orm.Insert(catm)
	if err != nil {
		log.Log.Error("保存分类<%d:%d:%s>出错，%s", catid, flag, relid, err)
		return err
	}

	return nil
}

func (p CategoryManager) ReleaseRelCategory(flag int, relid string) error {
	if flag <= 0 || len(relid) == 0 {
		return nil
	}
	_, err := models.Orm.QueryTable("CategorysMap").Filter("Flag", flag).Filter("RelId", relid).Delete()
	return err
}

func (p CategoryManager) GetRelCategory(flag int, relid string) (*models.Category, error) {
	m := &models.CategorysMap{}

	err := models.Orm.QueryTable("CategorysMap").Filter("Flag", flag).Filter("RelId", relid).One(m)
	if err != nil {
		log.Log.Error("读取<%d:%s>相关分类出错，%s", flag, relid, err)
		return nil, err
	}

	c := &models.Category{
		Id: m.CategoryId,
	}

	err = models.Orm.Read(c)
	if err != nil {
		log.Log.Error("读取<%d>分类出错，%s", m.CategoryId, err)
		return nil, err
	}
	return c, err

}

//SELECT a.category_id, b.name, b.parent_id, count(*) FROM mutu.categorys_map a left join mutu.category b on a.category_id=b.id group by a.category_id;

func (p CategoryManager) GetAllCategorysWithRelCount(flag int) ([]*models.Category, error) {
	var arr []*models.Category
	var sql string
	if flag <= 0 {
		sql = `SELECT a.category_id, b.name, b.parent_id, count(*) as count FROM mutu.categorys_map a left join mutu.category b on a.category_id=b.id group by a.category_id;`
	} else {
		sql = fmt.Sprintf(`SELECT a.category_id, b.name, b.parent_id, count(*) as count FROM mutu.categorys_map a left join mutu.category b on a.category_id=b.id where a.flag='%d' group by a.category_id;`,
			flag)
	}
	// r, err := models.Orm.Raw(sql).Exec()
	// fmt.Printf("resut:", ...)
	if _, err := models.Orm.Raw(sql).QueryRows(&arr); err != nil {
		log.Log.Error("读取分类信息及关联素材个数，读取失败，%s", err)
		return nil, err
	}
	fmt.Printf("resut: %+v", arr)

	return arr, nil
}
